

#include "OgreApplication.h"


#include "ProcesoVerificarMisiones.h"

#include "States.h"
#include <sstream>


void ProcesoVerificarMisiones::OnUpdate(int delta)
{
	//std::cout << "DeltaVM: " << delta << std::endl;
	m_tiempo += delta;


	if(m_tiempo > 2000)
	{
		m_tiempo = 0;

		// It isn't right to call OnInitialize directly, but it seems to work.
		// TODO: Check this.
		OnInitialize();

		Mision * mis = mMision.lock().get();
		if(mis == 0)
		{
			std::cout << "NULL MISSION (CHECK MISSIONS)" << std::endl;
			Finish(false);
			return;
		}

		bool completa = mis->EsCompletada();
		if(completa)
		{
			Finish();
			
			LuaEng::RunFile(OgreApplication::getInstance().GetLuaState(), "scripts/OnMisionCompletada.lua");
			LuaEng::EmpezarLlamada(OgreApplication::getInstance().GetLuaState(), "OnMisionCompletada");
			LuaEng::PushPointer(OgreApplication::getInstance().GetLuaState(), (void*)mis, "Mision *"); 
			LuaEng::TerminarLlamada(OgreApplication::getInstance().GetLuaState(), 1);
		
			ManagerDeMisiones::Get()->TerminarMision(mis->GetNombre());
		}
	}
}

void ProcesoVerificarMisiones::OnInitialize()
{
	MainGameState * est = GetMainGameState();
	Ogre::TextAreaOverlayElement * tex = est->GetOverlayObjetivos();
	
	Mision * mis = mMision.lock().get();
	if(!mis) return;

	std::stringstream oss;
	
	oss << "*** Current target ***" << std::endl;
	oss << mis->GetDescrip();

	tex->setCaption(oss.str());
}